package com.leo.base_project.activity.crash;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.leo.base_project.R;
import com.leo.base_project.activity.base.BaseActivityX;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* @program: com.leo.base_project.activity.crash
* @description: 全局异常捕获类
* @author: Leo
* @create: 2021/3/28 11:43
**/
@SuppressLint("NonConstantResourceId")
public class CrashActivity extends BaseActivityX {

    @BindView(R.id.tv_crash_info)
    TextView tvCrashInfo;
    @BindView(R.id.tv_crash_title)
    TextView tvCrashTitle;
    @BindView(R.id.tv_crash_message)
    TextView tvCrashMessage;
    @BindView(R.id.btn_crash)
    Button btnCrash;

    // 错误日志文件路径
    String filePath = "";
    // 附加信息（可选）
    String info = "";
    // 标题（可选）
    String title = "";



    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash);
        ButterKnife.bind(this);

        String message = getIntent().getStringExtra("message");
        filePath = getIntent().getStringExtra("filePath");
        info = getIntent().getStringExtra("info");
        title = getIntent().getStringExtra("title");
        // 异常信息
        if (!isEmpty(message))
            tvCrashMessage.setText(message);
        else {
            tvCrashMessage.setVisibility(View.GONE);
            btnCrash.setVisibility(View.GONE);
        }

        // 异常备注信息
        if (!isEmpty(info))
            tvCrashInfo.setText(info);
        else
            tvCrashInfo.setVisibility(View.GONE);

        // 标题
        if (!isEmpty(title))
            tvCrashTitle.setText(title);


//        "text/plain"
    }

    private boolean isEmpty(String string) {
        return string == null || StringUtils.isEmpty(string);
    }

    public void back(View view) {
        finish();
    }

    public void open(View view) {
//        File file = new File(filePath);
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            Uri apkUri = FileProvider.getUriForFile(this, "  com.example.railway.data.fileProvider", file);
//            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//            intent.setDataAndType(apkUri, "application/msword");
//        } else {
//            intent.setDataAndType(Uri.fromFile(file), "application/msword");
//        }
//        this.startActivity(intent);
    }
}